<!DOCTYPE html>
<html lang="${LANG}" class="dark">

<head>
    <meta charset="UTF-8">
    <title>QBT_TR(New Category)QBT_TR[CONTEXT=Category]</title>
    <link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css">
    <script defer src="scripts/localpreferences.js?v=${CACHEID}"></script>
    <script defer src="scripts/color-scheme.js?v=${CACHEID}"></script>
    <script defer src="scripts/misc.js?locale=${LANG}&v=${CACHEID}"></script>
    <script defer src="scripts/pathAutofill.js?v=${CACHEID}"></script>
    <script>
        "use strict";

        window.addEventListener("DOMContentLoaded", (event) => {
            window.addEventListener("keydown", (event) => {
                switch (event.key) {
                    case "Enter":
                        event.preventDefault();
                        document.getElementById("categoryNameButton").click();
                        break;
                    case "Escape":
                        event.preventDefault();
                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                        break;
                }
            });

            const defaultDownloadPath = () => {
                const category = document.getElementById("categoryName").value.trim();
                return `${pref.temp_path}/${category}`;
            };

            const setDownloadPath = (option) => {
                const downloadPath = document.getElementById("downloadPath");
                const defaultPath = defaultDownloadPath();
                switch (option) {
                    case "default":
                        downloadPath.disabled = true;
                        downloadPath.value = pref.temp_path_enabled ? defaultPath : "";
                        downloadPath.placeholder = "";
                        break;
                    case "yes":
                        downloadPath.disabled = false;
                        if ((categoryData !== undefined) && (categoryData.downloadPath !== false) && (categoryData.downloadPath !== null))
                            downloadPath.value = categoryData.downloadPath;
                        else
                            downloadPath.value = "";
                        downloadPath.placeholder = defaultPath;
                        break;
                    case "no":
                        downloadPath.disabled = true;
                        downloadPath.value = "";
                        downloadPath.placeholder = "";
                        break;
                }
            };

            const searchParams = new URLSearchParams(window.location.search);
            const uriAction = window.qBittorrent.Misc.safeTrim(searchParams.get("action"));
            const uriHashes = window.qBittorrent.Misc.safeTrim(searchParams.get("hashes"));
            const uriCategoryName = window.qBittorrent.Misc.safeTrim(searchParams.get("categoryName"));

            const pref = window.parent.qBittorrent.Cache.preferences.get();
            const categoryData = window.parent.qBittorrent.Client.categoryMap.get(uriCategoryName);

            const useDownloadPathElem = document.getElementById("useDownloadPath");
            const categoryNameElem = document.getElementById("categoryName");
            const savePathElem = document.getElementById("savePath");
            if (uriAction === "edit") {
                if (!uriCategoryName)
                    return;

                categoryNameElem.disabled = true;
                categoryNameElem.value = uriCategoryName;
                savePathElem.value = categoryData.savePath;
                savePathElem.placeholder = `${pref.save_path}/${uriCategoryName}`;
                savePathElem.focus();

                switch (categoryData.downloadPath) {
                    case false:
                        useDownloadPathElem.selectedIndex = 2;
                        setDownloadPath("no");
                        break;
                    case null:
                        useDownloadPathElem.selectedIndex = 0;
                        setDownloadPath("default");
                        break;
                    default:
                        useDownloadPathElem.selectedIndex = 1;
                        setDownloadPath("yes");
                        break;
                }
            }
            else if (uriAction === "createSubcategory") {
                categoryNameElem.value = uriCategoryName;
                categoryNameElem.focus();
            }
            else {
                categoryNameElem.focus();
            }

            document.getElementById("categoryNameButton").addEventListener("click", (e) => {
                e.preventDefault();
                e.stopPropagation();

                const savePath = document.getElementById("savePath").value.trim();
                const categoryName = document.getElementById("categoryName").value.trim();

                const verifyCategoryName = (name) => {
                    if ((name === null) || (name === ""))
                        return false;
                    if (name.match("^([^\\\\\\/]|[^\\\\\\/]([^\\\\\\/]|\\/(?=[^\\/]))*[^\\\\\\/])$") === null) {
                        alert("QBT_TR(Invalid category name:\nPlease do not use any special characters in the category name.)QBT_TR[CONTEXT=HttpServer]");
                        return false;
                    }
                    return true;
                };

                const dlPathParams = {};
                const useDownloadPath = document.getElementById("useDownloadPath");
                if (useDownloadPath.value === "no") {
                    dlPathParams.downloadPathEnabled = false;
                }
                else if (useDownloadPath.value === "yes") {
                    dlPathParams.downloadPathEnabled = true;
                    dlPathParams.downloadPath = document.getElementById("downloadPath").value.trim();
                }

                switch (uriAction) {
                    case "set":
                        if ((uriHashes === "") || !verifyCategoryName(categoryName))
                            return;

                        fetch("api/v2/torrents/createCategory", {
                                method: "POST",
                                body: new URLSearchParams({
                                    ...dlPathParams,
                                    category: categoryName,
                                    savePath: savePath
                                })
                            })
                            .then((response) => {
                                if (!response.ok) {
                                    alert(`QBT_TR(Unable to create category)QBT_TR[CONTEXT=Category] ${window.qBittorrent.Misc.escapeHtml(categoryName)}`);
                                    return;
                                }

                                fetch("api/v2/torrents/setCategory", {
                                        method: "POST",
                                        body: new URLSearchParams({
                                            hashes: uriHashes,
                                            category: categoryName
                                        })
                                    })
                                    .then((response) => {
                                        if (!response.ok) {
                                            alert("QBT_TR(Unable to set category)QBT_TR[CONTEXT=Category]");
                                            return;
                                        }

                                        window.parent.updateMainData();
                                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                                    });
                            });
                        break;

                    case "create":
                    case "createSubcategory":
                        if (!verifyCategoryName(categoryName))
                            return;
                        fetch("api/v2/torrents/createCategory", {
                                method: "POST",
                                body: new URLSearchParams({
                                    ...dlPathParams,
                                    category: categoryName,
                                    savePath: savePath
                                })
                            })
                            .then((response) => {
                                if (!response.ok) {
                                    alert("QBT_TR(Unable to create category)QBT_TR[CONTEXT=Category]");
                                    return;
                                }

                                window.parent.updateMainData();
                                window.parent.qBittorrent.Client.closeFrameWindow(window);
                            });
                        break;

                    case "edit":
                        fetch("api/v2/torrents/editCategory", {
                                method: "POST",
                                body: new URLSearchParams({
                                    ...dlPathParams,
                                    category: uriCategoryName, // category name can't be changed
                                    savePath: savePath
                                })
                            })
                            .then((response) => {
                                if (!response.ok) {
                                    alert("QBT_TR(Unable to edit category)QBT_TR[CONTEXT=Category]");
                                    return;
                                }

                                window.parent.updateMainData();
                                window.parent.qBittorrent.Client.closeFrameWindow(window);
                            });
                        break;
                }
            });

            useDownloadPathElem.addEventListener("change", (e) => {
                setDownloadPath(e.target.value);
            });

            window.qBittorrent.pathAutofill.attachPathAutofill();
        });
    </script>
</head>

<body>
    <div style="padding: 10px 10px 0px 10px;">
        <div style="display: flex; align-items: center;">
            <label for="categoryName">QBT_TR(Category:)QBT_TR[CONTEXT=Category]</label>
            <input type="text" id="categoryName" style="flex-grow: 1; margin-left: 10px;">
        </div>
        <div style="display: flex; align-items: center;">
            <label for="savePath">QBT_TR(Save path:)QBT_TR[CONTEXT=Category]</label>
            <input type="text" id="savePath" class="pathDirectory" style="flex-grow: 1; margin-left: 10px;">
        </div>

        <fieldset class="settings" style="text-align: left;">
            <legend>QBT_TR(Save path for incomplete torrents:)QBT_TR[CONTEXT=Category]</legend>

            <div style="display: flex; align-items: center;">
                <label for="useDownloadPath">QBT_TR(Use another path for incomplete torrents:)QBT_TR[CONTEXT=Category]</label>
                <select id="useDownloadPath">
                    <option selected value="default">QBT_TR(Default)QBT_TR[CONTEXT=Category]</option>
                    <option value="yes">QBT_TR(Yes)QBT_TR[CONTEXT=Category]</option>
                    <option value="no">QBT_TR(No)QBT_TR[CONTEXT=Category]</option>
                </select>
            </div>
            <div style="display: flex; align-items: center;">
                <label for="downloadPath">QBT_TR(Path:)QBT_TR[CONTEXT=Category]</label>
                <input type="text" id="downloadPath" class="pathDirectory" style="flex-grow: 1; margin-left: 10px;">
            </div>
        </fieldset>

        <div style="text-align: center; padding-top: 10px;">
            <input type="button" value="QBT_TR(OK)QBT_TR[CONTEXT=Category]" id="categoryNameButton">
        </div>
    </div>
</body>

</html>
